﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WinformWork
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("提示：请输入用户名和密码！", "警告");
            }
             
             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=bbtest;Integrated Security=True");
             conn.Open();
             SqlCommand cmd = new SqlCommand("select * from Student where Name='" + textBox1.Text.Trim() + "' and pasword='" + textBox2.Text.Trim() + "'", conn);
             SqlDataReader sdr = cmd.ExecuteReader();
             sdr.Read();
             if (sdr.HasRows)
            {
                MessageBox.Show("登录成功!", "提示");
            }

            else
            {
                MessageBox.Show("提示：学生用户名或密码错误!", "警告");
                conn.Close();
            }  
             

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
                MessageBox.Show("请输入用户名、密码！", "警告");
            else
            {
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=bbtest;Integrated Security=True");
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from Student where Name='" + textBox1.Text.Trim() + "'", conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                if (sdr.HasRows)
                {
                    MessageBox.Show("该用户已注册，请使用其他用户名", "提示");
                }
                    
                else
                {
                    sdr.Close();
                    string myinsert = "insert into Student values ('" + textBox1.Text + "','" + textBox2.Text + "')";
                    SqlCommand mycom = new SqlCommand(myinsert, conn);              
                    mycom.ExecuteNonQuery();  
                    conn.Close();     
                    conn.Dispose();
                    MessageBox.Show("您已注册成功！");

                }
            }
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            List<Color> color = new List<Color>();
            color.Add(Color.Red);
            color.Add(Color.Blue);
            color.Add(Color.Pink);
            color.Add(Color.Green);
            color.Add(Color.White);
            Random random = new Random();
            var ran = random.Next(0, color.Count);
            var randColor = color[ran];
            BackColor = randColor;
        }

        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            List<Color> color = new List<Color>();
            color.Add(Color.Blue);
            color.Add(Color.Pink);
            color.Add(Color.Green);
            Random random = new Random();
            var ran = random.Next(0, color.Count);
            var randColor = color[ran];
            BackColor = randColor;
        }
    }
}
